home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / gdata / urlfetch.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  8.6 KB  |  215 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """Provides HTTP functions for gdata.service to use on Google App Engine
  5.  
  6. AppEngineHttpClient: Provides an HTTP request method which uses App Engine's
  7.    urlfetch API. Set the http_client member of a GDataService object to an
  8.    instance of an AppEngineHttpClient to allow the gdata library to run on
  9.    Google App Engine.
  10.  
  11. run_on_appengine: Function which will modify an existing GDataService object
  12.    to allow it to run on App Engine. It works by creating a new instance of
  13.    the AppEngineHttpClient and replacing the GDataService object's 
  14.    http_client.
  15.  
  16. HttpRequest: Function that wraps google.appengine.api.urlfetch.Fetch in a 
  17.     common interface which is used by gdata.service.GDataService. In other 
  18.     words, this module can be used as the gdata service request handler so 
  19.     that all HTTP requests will be performed by the hosting Google App Engine
  20.     server. 
  21. """
  22. __author__ = 'api.jscudder (Jeff Scudder)'
  23. import StringIO
  24. import atom.service as atom
  25. import atom.http_interface as atom
  26. from google.appengine.api import urlfetch
  27.  
  28. def run_on_appengine(gdata_service):
  29.     '''Modifies a GDataService object to allow it to run on App Engine.
  30.  
  31.   Args:
  32.     gdata_service: An instance of AtomService, GDataService, or any
  33.         of their subclasses which has an http_client member.
  34.   '''
  35.     gdata_service.http_client = AppEngineHttpClient()
  36.  
  37.  
  38. class AppEngineHttpClient(atom.http_interface.GenericHttpClient):
  39.     
  40.     def __init__(self, headers = None):
  41.         self.debug = False
  42.         if not headers:
  43.             pass
  44.         self.headers = { }
  45.  
  46.     
  47.     def request(self, operation, url, data = None, headers = None):
  48.         """Performs an HTTP call to the server, supports GET, POST, PUT, and
  49.     DELETE.
  50.  
  51.     Usage example, perform and HTTP GET on http://www.google.com/:
  52.       import atom.http
  53.       client = atom.http.HttpClient()
  54.       http_response = client.request('GET', 'http://www.google.com/')
  55.  
  56.     Args:
  57.       operation: str The HTTP operation to be performed. This is usually one
  58.           of 'GET', 'POST', 'PUT', or 'DELETE'
  59.       data: filestream, list of parts, or other object which can be converted
  60.           to a string. Should be set to None when performing a GET or DELETE.
  61.           If data is a file-like object which can be read, this method will
  62.           read a chunk of 100K bytes at a time and send them.
  63.           If the data is a list of parts to be sent, each part will be
  64.           evaluated and sent.
  65.       url: The full URL to which the request should be sent. Can be a string
  66.           or atom.url.Url.
  67.       headers: dict of strings. HTTP headers which should be sent
  68.           in the request.
  69.     """
  70.         all_headers = self.headers.copy()
  71.         if headers:
  72.             all_headers.update(headers)
  73.         
  74.         data_str = data
  75.         if data:
  76.             if isinstance(data, list):
  77.                 converted_parts = [ _AppEngineHttpClient__ConvertDataPart(x) for x in data ]
  78.                 data_str = ''.join(converted_parts)
  79.             else:
  80.                 data_str = _AppEngineHttpClient__ConvertDataPart(data)
  81.         
  82.         if data and 'Content-Length' not in all_headers:
  83.             all_headers['Content-Length'] = len(data_str)
  84.         
  85.         if 'Content-Type' not in all_headers:
  86.             all_headers['Content-Type'] = 'application/atom+xml'
  87.         
  88.         if operation == 'GET':
  89.             method = urlfetch.GET
  90.         elif operation == 'POST':
  91.             method = urlfetch.POST
  92.         elif operation == 'PUT':
  93.             method = urlfetch.PUT
  94.         elif operation == 'DELETE':
  95.             method = urlfetch.DELETE
  96.         else:
  97.             method = None
  98.         return HttpResponse(urlfetch.Fetch(url = str(url), payload = data_str, method = method, headers = all_headers))
  99.  
  100.  
  101.  
  102. def HttpRequest(service, operation, data, uri, extra_headers = None, url_params = None, escape_params = True, content_type = 'application/atom+xml'):
  103.     """Performs an HTTP call to the server, supports GET, POST, PUT, and DELETE.
  104.  
  105.   This function is deprecated, use AppEngineHttpClient.request instead.
  106.  
  107.   To use this module with gdata.service, you can set this module to be the
  108.   http_request_handler so that HTTP requests use Google App Engine's urlfetch.
  109.   import gdata.service
  110.   import gdata.urlfetch
  111.   gdata.service.http_request_handler = gdata.urlfetch
  112.  
  113.   Args:
  114.     service: atom.AtomService object which contains some of the parameters
  115.         needed to make the request. The following members are used to
  116.         construct the HTTP call: server (str), additional_headers (dict),
  117.         port (int), and ssl (bool).
  118.     operation: str The HTTP operation to be performed. This is usually one of
  119.         'GET', 'POST', 'PUT', or 'DELETE'
  120.     data: filestream, list of parts, or other object which can be
  121.         converted to a string.
  122.         Should be set to None when performing a GET or PUT.
  123.         If data is a file-like object which can be read, this method will read
  124.         a chunk of 100K bytes at a time and send them.
  125.         If the data is a list of parts to be sent, each part will be evaluated
  126.         and sent.
  127.     uri: The beginning of the URL to which the request should be sent.
  128.         Examples: '/', '/base/feeds/snippets',
  129.         '/m8/feeds/contacts/default/base'
  130.     extra_headers: dict of strings. HTTP headers which should be sent
  131.         in the request. These headers are in addition to those stored in
  132.         service.additional_headers.
  133.     url_params: dict of strings. Key value pairs to be added to the URL as
  134.         URL parameters. For example {'foo':'bar', 'test':'param'} will
  135.         become ?foo=bar&test=param.
  136.     escape_params: bool default True. If true, the keys and values in
  137.         url_params will be URL escaped when the form is constructed
  138.         (Special characters converted to %XX form.)
  139.     content_type: str The MIME type for the data being sent. Defaults to
  140.         'application/atom+xml', this is only used if data is set.
  141.   """
  142.     full_uri = atom.service.BuildUri(uri, url_params, escape_params)
  143.     (server, port, ssl, partial_uri) = atom.service.ProcessUrl(service, full_uri)
  144.     if ssl:
  145.         full_url = 'https://%s%s' % (server, partial_uri)
  146.     else:
  147.         full_url = 'http://%s%s' % (server, partial_uri)
  148.     data_str = data
  149.     if data:
  150.         if isinstance(data, list):
  151.             converted_parts = [ __ConvertDataPart(x) for x in data ]
  152.             data_str = ''.join(converted_parts)
  153.         else:
  154.             data_str = __ConvertDataPart(data)
  155.     
  156.     headers = { }
  157.     if isinstance(service.additional_headers, dict):
  158.         headers = service.additional_headers.copy()
  159.     
  160.     if isinstance(extra_headers, dict):
  161.         for header, value in extra_headers.iteritems():
  162.             headers[header] = value
  163.         
  164.     
  165.     if content_type:
  166.         headers['Content-Type'] = content_type
  167.     
  168.     if operation == 'GET':
  169.         method = urlfetch.GET
  170.     elif operation == 'POST':
  171.         method = urlfetch.POST
  172.     elif operation == 'PUT':
  173.         method = urlfetch.PUT
  174.     elif operation == 'DELETE':
  175.         method = urlfetch.DELETE
  176.     else:
  177.         method = None
  178.     return HttpResponse(urlfetch.Fetch(url = full_url, payload = data_str, method = method, headers = headers))
  179.  
  180.  
  181. def __ConvertDataPart(data):
  182.     if not data or isinstance(data, str):
  183.         return data
  184.     if hasattr(data, 'read'):
  185.         return data.read()
  186.     return str(data)
  187.  
  188.  
  189. class HttpResponse(object):
  190.     '''Translates a urlfetch resoinse to look like an hhtplib resoinse.
  191.   
  192.   Used to allow the resoinse from HttpRequest to be usable by gdata.service
  193.   methods.
  194.   '''
  195.     
  196.     def __init__(self, urlfetch_response):
  197.         self.body = StringIO.StringIO(urlfetch_response.content)
  198.         self.headers = urlfetch_response.headers
  199.         self.status = urlfetch_response.status_code
  200.         self.reason = ''
  201.  
  202.     
  203.     def read(self, length = None):
  204.         if not length:
  205.             return self.body.read()
  206.         return self.body.read(length)
  207.  
  208.     
  209.     def getheader(self, name):
  210.         if not self.headers.has_key(name):
  211.             return self.headers[name.lower()]
  212.         return self.headers[name]
  213.  
  214.  
  215.